set more off
clear all

cap log close
log using an_enia_reg, t replace

*** runs OLS FE regs of performance on IP
*** sample provided by BHH on 6 Oct 2019
use sample, clear

keep id year ind logs logm loga loge tfp* dip dpat dtm dtfp dloge dlogs dlogm dloga ///
     foreign mixed public indiv export santiago hhi share4 shfor

qui tab ind,gen(i_)
global inddum i_2-i_18
qui tab year,gen(y_)
global yrdum y_3-y_10

gen logae = loga-loge
gen logse = logs-loge
gen logme = logm-loge
gen dexport = exports>0
gen loghhi = log(hhi)
gen logms = log(share4)
gen logfs = log(shfor)

gen mislogfs = (logfs==.)
replace logfs = 0 if logfs==.

do $CHDIR\cleanfile

global vars foreign public indiv dexport santiago logms loghhi logfs mislogfs
sum $vars logse loge logae logme dpat dtm

xtset id year
sort id year

gen dpatl1 = dpat[_n-1] 
gen dpatl2 = dpat[_n-2] if id==id[_n-2]
gen dtml1 = dtm[_n-1] if id==id[_n-1]
gen dtml2 = dtm[_n-2] if id==id[_n-2]
keep if id==id[_n-2]  /* drop the first 2 lagged obs to keep sample the same for all regs*/
sum $vars logse loge logae logme dpat dtm
 
**** patenting
xtreg logse loge logae logme dpat $yrdum, fe cluster(id)
est store patres1
xtreg logse loge logae logme dpat $vars $yrdum, fe cluster(id)
est store patres2

xtreg logse loge logae logme dpatl1 $yrdum, fe cluster(id)
est store patres3
xtreg logse loge logae logme dpatl1 $vars $yrdum, fe cluster(id)
est store patres4
 
xtreg logse loge logae logme dpatl2 $yrdum, fe cluster(id)
est store patres5
xtreg logse loge logae logme dpatl2 $vars $yrdum, fe cluster(id)
est store patres6

xtreg logse loge logae logme dpat dpatl1 dpatl2 $yrdum, fe cluster(id)
est store patres7
xtreg logse loge logae logme dpat dpatl1 dpatl2 $vars $yrdum, fe cluster(id)
est store patres8
lincom dpat+dpatl1+dpatl2

esttab patres*, keep(loge logae logme dpat dpatl1 dpatl2 $vars)       ///
                order(dpat dpatl1 dpatl2 loge logae logme $vars)      ///
         b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust)  ///
		 nogaps nolines compress

**** trademarking
xtreg logse loge logae logme dtm $yrdum, fe cluster(id)
est store tmres1
xtreg logse loge logae logme dtm $vars $yrdum, fe cluster(id)
est store tmres2

xtreg logse loge logae logme dtml1 $yrdum, fe cluster(id)
est store tmres3
xtreg logse loge logae logme dtml1 $vars $yrdum, fe cluster(id)
est store tmres4
 
xtreg logse loge logae logme dtml2 $yrdum, fe cluster(id)
est store tmres5
xtreg logse loge logae logme dtml2 $vars $yrdum, fe cluster(id)
est store tmres6

xtreg logse loge logae logme dtm dtml1 dtml2 $yrdum, fe cluster(id)
est store tmres7
xtreg logse loge logae logme dtm dtml1 dtml2 $vars $yrdum, fe cluster(id)
est store tmres8
lincom dtm+dtml1+dtml2

esttab tmres*, keep(loge logae logme dtm dtml1 dtml2 $vars)           ///
               order(dtm dtml1 dtml2 loge logae logme $vars)          ///
         b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust)  ///
		 nogaps nolines compress

log close
		 